from flask_restful import Resource, reqparse
from flask import request
from models import db, PendingDish
from utils import api_response

class PendingDishList(Resource):
    def get(self):
        """获取待做菜品列表"""
        parser = reqparse.RequestParser()
        parser.add_argument('family_id', type=int, location='args')
        parser.add_argument('status', type=int, location='args')
        parser.add_argument('page', type=int, default=1, location='args')
        parser.add_argument('size', type=int, default=10, location='args')
        args = parser.parse_args()
        
        query = PendingDish.query
        if args['family_id'] is not None:
            query = query.filter_by(family_id=args['family_id'])
        if args['status'] is not None:
            query = query.filter_by(status=args['status'])
        
        pending_dishes = query.paginate(
            page=args['page'], 
            per_page=args['size'], 
            error_out=False
        )
        
        return api_response({
            'total': pending_dishes.total,
            'page': args['page'],
            'size': args['size'],
            'pending_dishes': [dish.to_dict() for dish in pending_dishes.items]
        })
    
    def post(self):
        """创建待做菜品"""
        parser = reqparse.RequestParser()
        parser.add_argument('family_id', type=int, required=True, help='家庭ID不能为空')
        parser.add_argument('dish_name', type=str, required=True, help='菜品名称不能为空')
        parser.add_argument('quantity', type=int, default=1)
        parser.add_argument('remarks', type=str)
        parser.add_argument('meal_type', type=str, required=True, help='餐次类型不能为空')
        parser.add_argument('status', type=int, default=0)
        args = parser.parse_args()
        
        pending_dish = PendingDish(
            family_id=args['family_id'],
            dish_name=args['dish_name'],
            quantity=args['quantity'],
            remarks=args['remarks'],
            meal_type=args['meal_type'],
            status=args['status']
        )
        
        db.session.add(pending_dish)
        db.session.commit()
        
        return api_response(pending_dish.to_dict(), message='待做菜品创建成功')

class PendingDishDetail(Resource):
    def get(self, pending_dish_id):
        """获取待做菜品详情"""
        pending_dish = PendingDish.query.get(pending_dish_id)
        if not pending_dish:
            return api_response({'message': '待做菜品不存在'}, code=404)
        
        return api_response(pending_dish.to_dict())
    
    def put(self, pending_dish_id):
        """更新待做菜品"""
        pending_dish = PendingDish.query.get(pending_dish_id)
        if not pending_dish:
            return api_response({'message': '待做菜品不存在'}, code=404)
        
        parser = reqparse.RequestParser()
        parser.add_argument('dish_name', type=str)
        parser.add_argument('quantity', type=int)
        parser.add_argument('remarks', type=str)
        parser.add_argument('meal_type', type=str)
        parser.add_argument('status', type=int)
        args = parser.parse_args()
        
        if args['dish_name'] is not None:
            pending_dish.dish_name = args['dish_name']
        if args['quantity'] is not None:
            pending_dish.quantity = args['quantity']
        if args['remarks'] is not None:
            pending_dish.remarks = args['remarks']
        if args['meal_type'] is not None:
            pending_dish.meal_type = args['meal_type']
        if args['status'] is not None:
            pending_dish.status = args['status']
        
        db.session.commit()
        
        return api_response(pending_dish.to_dict(), message='待做菜品更新成功')
    
    def delete(self, pending_dish_id):
        """删除待做菜品"""
        pending_dish = PendingDish.query.get(pending_dish_id)
        if not pending_dish:
            return api_response({'message': '待做菜品不存在'}, code=404)
        
        db.session.delete(pending_dish)
        db.session.commit()
        
        return api_response(None, message='待做菜品删除成功')